{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "\n",
    "from datasets import load_dataset\n",
    "\n",
    "ds = load_dataset(\"deepmind/code_contests\", split=\"train\")\n",
    "print(ds)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "TEMPLATE = \"\"\"\n",
    "{problem}\n",
    "\n",
    "Time Limit: {time_limit}\n",
    "Memory Limit: {memory_limit} Bytes\n",
    "\"\"\"\n",
    "\n",
    "dataset = []\n",
    "for entry in ds:\n",
    "    difficulty = entry[\"difficulty\"]  # TODO: understand how the difficulty is calculated and convert the difficulty to a number from 1-10 or 0 for unknown\n",
    "\n",
    "    if not (difficulty >= 2 and difficulty <= 5) and not (difficulty >= 10):\n",
    "        continue\n",
    "\n",
    "    input_tests = entry[\"public_tests\"][\"input\"] + entry[\"private_tests\"][\"input\"]\n",
    "    output_tests = entry[\"public_tests\"][\"output\"] + entry[\"private_tests\"][\"output\"]\n",
    "\n",
    "    if len(input_tests) <= 1:\n",
    "        continue\n",
    "    assert len(input_tests) == len(output_tests)\n",
    "\n",
    "    full_tests = {\n",
    "        \"inputs\": input_tests,\n",
    "        \"outputs\": output_tests,\n",
    "    }\n",
    "    new_entry = {\n",
    "        \"problem\": TEMPLATE.format(problem=entry[\"description\"], time_limit=entry[\"time_limit\"], memory_limit=entry[\"memory_limit_bytes\"]),\n",
    "        \"tests\": full_tests,\n",
    "    }\n",
    "    dataset.append(new_entry)\n",
    "\n",
    "print(len(dataset))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "output_dir = os.path.abspath(\"../../train/code\")\n",
    "output_file = os.path.join(output_dir, \"code_contests.json\")\n",
    "\n",
    "with open(output_file, \"w\") as f:\n",
    "    json.dump(dataset, f, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ds = load_dataset(\"deepmind/code_contests\", split=\"test\")\n",
    "\n",
    "\n",
    "dataset = []\n",
    "for entry in ds:\n",
    "    difficulty = entry[\"difficulty\"]  # TODO: understand how the difficulty is calculated and convert the difficulty to a number from 1-10 or 0 for unknown\n",
    "\n",
    "    input_tests = entry[\"public_tests\"][\"input\"] + entry[\"private_tests\"][\"input\"]\n",
    "    output_tests = entry[\"public_tests\"][\"output\"] + entry[\"private_tests\"][\"output\"]\n",
    "\n",
    "    full_tests = {\n",
    "        \"inputs\": input_tests,\n",
    "        \"outputs\": output_tests,\n",
    "    }\n",
    "\n",
    "    new_entry = {\"problem\": TEMPLATE.format(problem=entry[\"description\"], time_limit=entry[\"time_limit\"], memory_limit=entry[\"memory_limit_bytes\"]), \"tests\": full_tests}\n",
    "    dataset.append(new_entry)\n",
    "\n",
    "print(len(dataset))\n",
    "\n",
    "output_dir = os.path.abspath(\"../../test/code\")\n",
    "output_file = os.path.join(output_dir, \"code_contests.json\")\n",
    "\n",
    "with open(output_file, \"w\") as f:\n",
    "    json.dump(dataset, f, indent=4)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "rllm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
